"""
@author: wangkang
@contact: wangkang@autobio.com.cn
@file: question_parser.py
@time: 2020/4/1 18:05
@desc: 问句分类
"""


class QuestionPaser:
    '''构建实体节点'''

    def build_entitydict(self, args):
        entity_dict = {}
        for arg, types in args.items():
            for type in types:
                if type not in entity_dict:
                    entity_dict[type] = [arg]
                else:
                    entity_dict[type].append(arg)
        return entity_dict

    def sql_transfer(self, question_type, entities):
        if not entities:
            return []
        # 查询语句
        sql = []

        # 查询某单元会有哪些故障
        if question_type == 'unit_apparance':
            sql = ["MATCH (m:UNIT)-[r:apparance]->(n:APPARANCE) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in
                   entities]

        # 查询某类型设备有哪些单元组成
        elif question_type == 'ins_type_have':
            sql = ["MATCH (m:INSTRUMENT)-[r:have]->(n:UNIT) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in
                   entities]

        # 查询单元维护保养手段
        elif question_type == 'unit_method':
            sql = ["MATCH (m:UNIT)-[r:method]->(n:METHOD) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]

        # 查询单元有哪些子部件（子部件属于哪个单元）
        elif question_type == 'unit_have':
            sql = ["MATCH (m:SUB_UNIT)-[r:belong]->(n:UNIT) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]

        elif question_type == 'sub_unit_belong':
            sql = ["MATCH (m:SUB_UNIT)-[r:belong]->(n:UNIT) where m.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]

        # 查询某保养手段可以用在哪些单元
        elif question_type == 'method_apply':
            sql = ["MATCH (m:UNIT)-[r:method]->(n:METHOD) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]

        # 查询某保故障出现在哪些单元
        elif question_type == 'appear_on':
            sql = ["MATCH (m:UNIT)-[r:apparance]->(n:APPARANCE) where n.name = '{0}' return m.name, r.name, n.name".format(i) for i in entities]

        return sql

    def parser_main(self, res_classify):
        """
        解析主函数
        :param res_classify:
        :return:
        """
        # 找到实体属于6大类中哪几类
        args = res_classify['args']
        entity_dict = self.build_entitydict(args)
        question_types = res_classify['question_types']
        sqls = []
        for question_type in question_types:
            sql_ = {}
            sql_['question_type'] = question_type
            sql = []
            if question_type == 'unit_apparance':
                sql = self.sql_transfer(question_type, entity_dict.get('UNIT'))

            elif question_type == 'ins_type_have':
                sql = self.sql_transfer(question_type, entity_dict.get('INSTRUMENT'))

            elif question_type == 'unit_method':
                sql = self.sql_transfer(question_type, entity_dict.get('UNIT'))

            elif question_type == 'unit_have':
                sql = self.sql_transfer(question_type, entity_dict.get('UNIT'))

            elif question_type == 'sub_unit_belong':
                sql = self.sql_transfer(question_type, entity_dict.get('SUB_UNIT'))

            elif question_type == 'method_apply':
                sql = self.sql_transfer(question_type, entity_dict.get('METHOD'))

            elif question_type == 'appear_on':
                sql = self.sql_transfer(question_type, entity_dict.get('APPARANCE'))

            if sql:
                sql_['sql'] = sql

                sqls.append(sql_)

        return sqls

    '''针对不同的问题，分开进行处理'''


if __name__ == '__main__':
    handler = QuestionPaser()
